<template>
  <div class="container">
    <el-row type="flex" class="row-bg" justify="space-between" align="middle">
      <el-col :span="6"><h1>科室管理模块</h1></el-col>
      <el-col :span="6" style="text-align: right;">
        <el-button type="danger" @click="handleDeleteMultiple">删除</el-button>
        <el-button type="primary" @click="dialogVisible = true">新增科室信息</el-button>
      </el-col>
    </el-row>

    <el-card class="box-card">
      <el-row type="flex" align="middle" :gutter="20" style="margin-bottom: 50px;">
        <el-col :span="6">
          <el-input v-model="searchVal" placeholder="请输入科室名称" />
        </el-col>
        <el-col :span="18">
          <el-button type="primary" @click="getAdminList">搜索</el-button>
        </el-col>
      </el-row>

      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="科室名称" />
        <el-table-column prop="campus" label="院区" />
        <el-table-column prop="departmentLocation" label="科室位置" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle style="margin-right: 10px;" @click="editDepartment(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="deleteDepartment(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" align="middle" style="margin-top: 20px;">
        <el-col :span="24" style="text-align: right;">
          <el-pagination
            :current-page="listQuery.pageNum"
            :page-sizes="[10, 20, 30]"
            :page-size="listQuery.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listQuery.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>

    </el-card>

    <!-- 新增科室对话框 -->
    <el-dialog title="新增科室信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="科室名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="院区">
          <el-input v-model="form.campus"></el-input>
        </el-form-item>
        <el-form-item label="科室位置">
          <el-input v-model="form.departmentLocation"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDepartment">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改科室对话框 -->
    <el-dialog title="修改科室信息" :visible.sync="editDialogVisible" width="30%">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="科室名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="院区">
          <el-input v-model="editForm.campus"></el-input>
        </el-form-item>
        <el-form-item label="科室位置">
          <el-input v-model="editForm.departmentLocation"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="editForm.status" placeholder="请选择状态">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateDepartment">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAlldepartment, createdepartment, deletedepartment, updatedepartment } from '@/api/department'

export default {
  data() {
    return {
      searchVal: '', // 搜索框查询
      tableData: [], // 列表数据
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      dialogVisible: false, // 控制新增医生弹窗的显示与隐藏
      editDialogVisible: false, // 控制修改医生弹窗的显示与隐藏
      form: {
        name: '',
        campus: '',
        departmentLocation: '',
        status: '',
      },
      editForm: {
        id: '',
        name: '',
        campus: '',
        departmentLocation: '',
        status: '',
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getAlldepartment()
    },
    switchStatus(status) {
      console.log(status)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    handleSelectionChange(e) {
      console.log(e)
    },
    getAlldepartment() {
      const that = this
      getAlldepartment().then(res => {
        const data = res['data']
        that.listQuery.total = data['total']
        that.tableData = data['list']
        console.log(res)
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    handleAvatarSuccess(res, file) {
      this.form.avatar = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    addDepartment() {
      // 提交表单数据
      createdepartment(this.form).then(res => {
        this.$message.success('新增科室成功')
        this.dialogVisible = false
        this.getAlldepartment() // 刷新列表
        console.log(res)
      }).catch(error => {
        this.$message.error('新增科室失败')
        console.error(error)
      })
    },
    deleteDepartment(id) {
      this.$confirm('此操作将永久删除该医生信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用 API 删除医生
        deletedepartment(id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 刷新列表
          this.getAlldepartment()
        }).catch((error) => {
          this.$message.error('删除失败')
          console.error(error)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editDepartment(row) {
      this.editForm = { ...row }
      this.editDialogVisible = true
    },
    updateDepartment() {
      const id = this.editForm.id
      const data = {
        name: this.editForm.name,
        campus: this.editForm.campus,
        departmentLocation: this.editForm.departmentLocation,
        status: this.editForm.status
      }
      updatedepartment(id, data).then(() => {
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
        this.editDialogVisible = false
        this.getAlldepartment() // 刷新列表
      }).catch((error) => {
        this.$message.error('修改失败')
        console.error(error)
      })
    }
  }
}
</script>

<style scoped>
/* 您可以在这里添加样式 */
</style>
